package com.changyue.activity.history.entity;

import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;

import com.changyue.bleHelper.BLEConstant;
import com.changyue.bleHelper.BLEDeviceConfig;
import com.changyue.blood.R;

import com.changyue.util.EmptyUtil;

import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.JoinProperty;
import org.greenrobot.greendao.annotation.ToMany;

import java.util.List;

import org.greenrobot.greendao.annotation.Generated;
import org.greenrobot.greendao.DaoException;
import com.changyue.greendao.gen.DaoSession;
import com.changyue.greendao.gen.TestDataEntityDao;
import com.changyue.greendao.gen.HistoryDataEntityDao;

@Entity
public class HistoryDataEntity implements Parcelable {

    @Id(autoincrement = true)
    Long id;

    //检查类型
    public int DETECTION_TYPE;
    public int TEMPERATURE;

    //芯片插入状态
    public int BOXSTATE;
    //通道数
    public int CHANNELS;
    public int ADC1;
    public int ADC2;

    public String save_date;
    public Integer workMethod;

    @ToMany(joinProperties = {@JoinProperty(name = "id", referencedName = "data_id")})
    public List<TestDataEntity> lineDatas;

    public HistoryDataEntity() {
        super();
        DETECTION_TYPE = -1;
        CHANNELS = 0;
        BOXSTATE = 0;
        TEMPERATURE = 0;
        ADC1 = 0;
        ADC2 = 0;

        lineDatas = null;
        workMethod = 0x001;
    }

    protected HistoryDataEntity(Parcel in) {
        DETECTION_TYPE = in.readInt();
        TEMPERATURE = in.readInt();
        BOXSTATE = in.readInt();
        CHANNELS = in.readInt();
        ADC1 = in.readInt();
        ADC2 = in.readInt();
        save_date = in.readString();
        lineDatas = in.createTypedArrayList(TestDataEntity.CREATOR);
        workMethod = in.readInt();
    }

    @Generated(hash = 63477604)
    public HistoryDataEntity(Long id, int DETECTION_TYPE, int TEMPERATURE, int BOXSTATE, int CHANNELS, int ADC1, int ADC2,
            String save_date, Integer workMethod) {
        this.id = id;
        this.DETECTION_TYPE = DETECTION_TYPE;
        this.TEMPERATURE = TEMPERATURE;
        this.BOXSTATE = BOXSTATE;
        this.CHANNELS = CHANNELS;
        this.ADC1 = ADC1;
        this.ADC2 = ADC2;
        this.save_date = save_date;
        this.workMethod = workMethod;
    }

    public static final Creator<HistoryDataEntity> CREATOR = new Creator<HistoryDataEntity>() {
        @Override
        public HistoryDataEntity createFromParcel(Parcel in) {
            return new HistoryDataEntity(in);
        }

        @Override
        public HistoryDataEntity[] newArray(int size) {
            return new HistoryDataEntity[size];
        }
    };

    /** Used to resolve relations */
    @Generated(hash = 2040040024)
    private transient DaoSession daoSession;

    /** Used for active entity operations. */
    @Generated(hash = 391510513)
    private transient HistoryDataEntityDao myDao;



    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(DETECTION_TYPE);
        dest.writeInt(TEMPERATURE);
        dest.writeInt(BOXSTATE);
        dest.writeInt(CHANNELS);
        dest.writeInt(ADC1);
        dest.writeInt(ADC2);
        dest.writeString(save_date);
        dest.writeTypedList(lineDatas);
        dest.writeInt(workMethod);
    }

    public String getResultInfo(Context context) {

        StringBuilder result = new StringBuilder();
        result.append(context.getString(R.string.detection_type) + ":" + BLEDeviceConfig.getInstance().typeName);
        result.append("\n");
        result.append(context.getString(R.string.work_method) + ":" + BLEConstant.get_WORK_METHOD_swv_cmd_name(workMethod));
        result.append("\n");
        result.append(context.getString(R.string.temperature) + ":" + TEMPERATURE + "℃");
        result.append("\n");
        if (BOXSTATE == 0x01) {
            result.append(context.getString(R.string.box_state) + ":" + context.getString(R.string.plugged));
            result.append("\n");
        } else if (BOXSTATE == 0x02) {
            result.append(context.getString(R.string.box_state) + ":" + context.getString(R.string.unplugged));
            result.append("\n");
        } else {
            result.append(context.getString(R.string.box_state) + ":" + context.getString(R.string.unknown));
            result.append("\n");
        }

        result.append("\n");
        result.append(context.getString(R.string.date) + ":" + save_date);

//        result.append(getString(R.string.lbs) + ":" + "宁波");
//        result.append("\n");

        return result.toString();
    }

    public String getCSVString(Context context) {

        StringBuffer buffer = new StringBuffer();

        buffer.append("\n");
        buffer.append("数据\n");

        String dataTitle = ",";
        String data1 = "通道一,";
        String data2 = "通道二,";

        if (EmptyUtil.isNotEmpty(lineDatas)) {
            for (int i = 0; i < lineDatas.size(); i++) {
                TestDataEntity testDataEntity = lineDatas.get(i);
                dataTitle = dataTitle + (i+1) + ",";
                data1 = data1 + testDataEntity.ch1 + ",";
                data2 = data2 + testDataEntity.ch2 + ",";
            }
        }

        buffer.append(dataTitle + "\n");
        buffer.append(data1 + "\n");
        buffer.append(data2 + "\n");

        buffer.append("\n");
        buffer.append("\n");
        buffer.append("测量时间," + save_date + "\n");
        buffer.append("温度," + TEMPERATURE + "\n");

        if (BOXSTATE == 0x01) {
            buffer.append(context.getString(R.string.box_state) + "," + context.getString(R.string.plugged) + "\n");
        } else if (BOXSTATE == 0x02) {
            buffer.append(context.getString(R.string.box_state) + "," + context.getString(R.string.unplugged) + "\n");
        } else {
            buffer.append(context.getString(R.string.box_state) + "," + context.getString(R.string.unknown) + "\n");
        }

        buffer.append(context.getString(R.string.channels) + "," + CHANNELS + "\n");
        buffer.append(context.getString(R.string.adc1) + "," + ADC1 + "nA" + "\n");
        buffer.append(context.getString(R.string.adc2) + "," + ADC2 + "nA" + "\n");

        return buffer.toString();

    }

    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public int getDETECTION_TYPE() {
        return this.DETECTION_TYPE;
    }

    public void setDETECTION_TYPE(int DETECTION_TYPE) {
        this.DETECTION_TYPE = DETECTION_TYPE;
    }

    public int getTEMPERATURE() {
        return this.TEMPERATURE;
    }

    public void setTEMPERATURE(int TEMPERATURE) {
        this.TEMPERATURE = TEMPERATURE;
    }

    public int getBOXSTATE() {
        return this.BOXSTATE;
    }

    public void setBOXSTATE(int BOXSTATE) {
        this.BOXSTATE = BOXSTATE;
    }

    public int getCHANNELS() {
        return this.CHANNELS;
    }

    public void setCHANNELS(int CHANNELS) {
        this.CHANNELS = CHANNELS;
    }

    public int getADC1() {
        return this.ADC1;
    }

    public void setADC1(int ADC1) {
        this.ADC1 = ADC1;
    }

    public int getADC2() {
        return this.ADC2;
    }

    public void setADC2(int ADC2) {
        this.ADC2 = ADC2;
    }

    public String getSave_date() {
        return this.save_date;
    }

    public void setSave_date(String save_date) {
        this.save_date = save_date;
    }

    public Integer getWorkMethod() {
        return this.workMethod;
    }

    public void setWorkMethod(Integer workMethod) {
        this.workMethod = workMethod;
    }

    /**
     * To-many relationship, resolved on first access (and after reset).
     * Changes to to-many relations are not persisted, make changes to the target entity.
     */
    @Generated(hash = 61625046)
    public List<TestDataEntity> getLineDatas() {
        if (lineDatas == null) {
            final DaoSession daoSession = this.daoSession;
            if (daoSession == null) {
                throw new DaoException("Entity is detached from DAO context");
            }
            TestDataEntityDao targetDao = daoSession.getTestDataEntityDao();
            List<TestDataEntity> lineDatasNew = targetDao._queryHistoryDataEntity_LineDatas(id);
            synchronized (this) {
                if (lineDatas == null) {
                    lineDatas = lineDatasNew;
                }
            }
        }
        return lineDatas;
    }

    /** Resets a to-many relationship, making the next get call to query for a fresh result. */
    @Generated(hash = 783215501)
    public synchronized void resetLineDatas() {
        lineDatas = null;
    }

    /**
     * Convenient call for {@link org.greenrobot.greendao.AbstractDao#delete(Object)}.
     * Entity must attached to an entity context.
     */
    @Generated(hash = 128553479)
    public void delete() {
        if (myDao == null) {
            throw new DaoException("Entity is detached from DAO context");
        }
        myDao.delete(this);
    }

    /**
     * Convenient call for {@link org.greenrobot.greendao.AbstractDao#refresh(Object)}.
     * Entity must attached to an entity context.
     */
    @Generated(hash = 1942392019)
    public void refresh() {
        if (myDao == null) {
            throw new DaoException("Entity is detached from DAO context");
        }
        myDao.refresh(this);
    }

    /**
     * Convenient call for {@link org.greenrobot.greendao.AbstractDao#update(Object)}.
     * Entity must attached to an entity context.
     */
    @Generated(hash = 713229351)
    public void update() {
        if (myDao == null) {
            throw new DaoException("Entity is detached from DAO context");
        }
        myDao.update(this);
    }

    /** called by internal mechanisms, do not call yourself. */
    @Generated(hash = 1597081233)
    public void __setDaoSession(DaoSession daoSession) {
        this.daoSession = daoSession;
        myDao = daoSession != null ? daoSession.getHistoryDataEntityDao() : null;
    }

}

